#!/usr/bin/perl

use strict;
use warnings;

# Load testing framework and use 'no_plan' to dynamically pick up
# all tests. Better to replace "'no_plan'" with "tests => 30" so it
# knows exactly how many tests need to be run (and will tell you if
# not), but 'no_plan' is nice for quick & dirty tests

#use Test::More 'no_plan';
use Test::More tests=>16;

# Need to specify the name of your app as arg on next line
# Can also do:
#   use Test::WWW::Mechanize::Catalyst "MyApp";

use ok "Test::WWW::Mechanize::Catalyst" => "Simpleblog";
    
# Create two 'user agents' to simulate two different users ('test01' & 'test02')
my $ua1 = Test::WWW::Mechanize::Catalyst->new;
my $ua2 = Test::WWW::Mechanize::Catalyst->new;

# Use a simplified for loop to do tests that are common to both users
# Use get_ok() to make sure we can hit the base URL
# Second arg = optional description of test (will be displayed for failed tests)
# Note that in test scripts you send everything to 'http://localhost'
$_->get_ok("http://localhost/blog/", "Check redirect of base URL") for $ua1, $ua2;
# Use title_is() to check the contents of the <title>...</title> tags
$_->title_is("Login", "Check for login title") for $ua1, $ua2;
# Use content_contains() to match on text in the html body
$_->content_contains("Username",
    "Check we are NOT logged in") for $ua1, $ua2;

# Log in as each user
# Specify username and password on the URL
$ua1->get_ok("http://localhost/logon/login?username=test1&password=test1", "Login 'test01'");
# Use the form for user 'test02'; note there is no description here
$ua2->submit_form(
    fields => {
        username => 'test2',
        password => 'test2',
    });

# goto index page after login
$ua1->title_is("Blogs", "We goto index page after login");
# goto last request url /blog after login
$ua2->content_contains("Matched Simpleblog::Controller::Blog in Blog","goto http://localhost/blog/");




# create a new blog now.
my $random = rand(10000);
$_->get_ok("http://localhost/blog/create") for $ua1, $ua2;
$ua1->submit_form(fields => {subject =>''});
$ua1->content_contains('<span class="all_errors" id="blog_form_subject_error_all">Required.',"check post blog error ");
$ua1->submit_form(fields =>{subject => $random, body=>$random});
$ua1->content_contains("blog created", "check whether post succeed." );
$ua2->get_ok("http://localhost/");
$ua2->content_contains($random);



